package DAO;

import java.sql.Connection;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;

import Model.Hemodialise;
import Utils.Conexao;

public class HemodialiseDAO {
	
	public static void Insert(Hemodialise hemodialise) throws SQLException
	{
		Connection con = Conexao.getConection();		
		String mysqlCall = "call spc_insert_hemodialise(?, ?, ?, ?, ?, ?, ?, ?, ?)";			
		CallableStatement call = HemodialiseDAO.setObjectCallableStatement(hemodialise, mysqlCall, con);		
		call.execute();
		
		con.close();		
		call.close();
	}
	
	public static void Update(Hemodialise hemodialise) throws SQLException
	{		
		Connection con = Conexao.getConection();		
		String mysqlCall = "call spc_update_sessao_hemodialise(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";		
		CallableStatement call = HemodialiseDAO.setObjectCallableStatement(hemodialise, mysqlCall, con) ;
		call.execute();
		
		con.close();
		call.close();
	}
	
	public static Hemodialise LoadObject(int idHemodialise) throws SQLException
	{
		Hemodialise hemodialise = null;
		Connection con = Conexao.getConection();		
		String mysqlCall = "call spc_select_hemodialise_por_id(?)";
		
		CallableStatement call =(CallableStatement) con.prepareCall(mysqlCall);
		call.setInt(1, idHemodialise);
		ResultSet res = call.executeQuery();
		
		if(res.next())
			hemodialise = HemodialiseDAO.SetInstance(res);
		
		con.close();
		call.close();
		
		return hemodialise;
	}
	
	public static void InativarSessao(int id_sessao) throws SQLException
	{		
		Connection con = Conexao.getConection();
		String mysqlCall = "call spc_inativar_hemodialise(?)";
		CallableStatement call =(CallableStatement) con.prepareCall(mysqlCall);		
		call.setInt(1, id_sessao);
		call.executeUpdate();
		
		call.close();
		con.close();
		
	}
	
	public static ResultSet relatorioHemodialise(java.sql.Date data_inicio, java.sql.Date data_fim) throws SQLException
	{
		Connection con = Conexao.getConection();		
		String mysqlCall = "call spc_relatorio_hemodialise(?, ?)";
		CallableStatement call = con.prepareCall(mysqlCall);
		call.setDate(1, data_inicio);
		call.setDate(2, data_fim);				
		return call.executeQuery();
	}
	
	public static ArrayList<Hemodialise> GetArrayListHemodialise()
	{
		ArrayList<Hemodialise> arrHemodialise = new ArrayList<Hemodialise>();
		Connection con = Conexao.getConection();		
		
		try {
			String mysqlCall = "call spc_select_sessoes_hemodialises()";
			
			CallableStatement call = (CallableStatement) con.prepareCall(mysqlCall);
			ResultSet res = call.executeQuery();
			
			while(res.next())
			{
				Hemodialise hemodialise = HemodialiseDAO.SetInstance(res);
				arrHemodialise.add(hemodialise);
			}
			
			con.close();
			call.close();
			
		} 
		catch (SQLException e)
		{			
			e.printStackTrace();
		}
		
		return arrHemodialise;
	}
	
	private static CallableStatement setObjectCallableStatement(Hemodialise hemodialise, String mysqlCall, Connection con ) throws SQLException
	{		
		
		CallableStatement call = (CallableStatement) con.prepareCall(mysqlCall);
		call.setDate(1, hemodialise.getDataSessao() );
		call.setTime(2, hemodialise.getHoraInicio());
		call.setTime(3, hemodialise.getHoraFim());
		call.setDouble(4, hemodialise.getPesoPre());
		call.setDouble(5, hemodialise.getPesoPos());
		call.setString(6, hemodialise.getResponsavel());
		call.setString(7, hemodialise.getObservacao());
		call.setInt(8, hemodialise.getIdUsuario());
		call.setInt(9, hemodialise.getIdPaciente());
		
		if(hemodialise.getIdHemodialise() > 0)
			call.setInt(10, hemodialise.getIdHemodialise());
	
		return call; 		
	}
	
	private static Hemodialise SetInstance(ResultSet res) throws SQLException
	{
		Hemodialise hemodialise = new Hemodialise();

		hemodialise.setIdHemodialise(res.getInt("id_hemodialise"));
		hemodialise.setDataSessao(res.getDate("data_sessao"));
		hemodialise.setHoraInicio(res.getTime("hora_inicio"));
		hemodialise.setHoraFim(res.getTime("hora_fim"));
		hemodialise.setPesoPre(res.getDouble("peso_pre"));
		hemodialise.setPesoPos(res.getDouble("peso_pos"));
		hemodialise.setResponsavel(res.getString("responsavel"));
		hemodialise.setObservacao(res.getString("observacao"));
		hemodialise.setIdPaciente(res.getInt("id_paciente"));
		hemodialise.setIdUsuario(res.getInt("id_usuario"));
		hemodialise.setPaciente(PacienteDAO.LoadObject(res.getInt("id_paciente")));
		hemodialise.setUsuario(UsuarioDAO.LoadObject(res.getInt("id_usuario")));
		
		return hemodialise;
				
	}
}
